<?php

namespace app\api\service;

use app\api\model\ProductAttrModel;
use think\Db;
use think\Exception;
use think\Request;
use think\Validate;

class ProductAttrService extends Api
{
    protected static $instance;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    public static function getInstance()
    {
        if (is_null(self::$instance))
        {
            self::$instance = new static();
        }
        return self::$instance;
    }

    /**
     * 变更产品特殊属性
     */
    public function changeProAttr($params)
    {
        $Validate = new Validate([
            'title' => 'require',
            'status' => 'require'
        ], [
            'title' => '属性名称必填',
            'status' => '请设置属性状态'
        ]);
        if (!$Validate->check($params))
        {
            $this->error($Validate->getError());
        }

        Db::startTrans();
        try{
            $saveData = [
                'title' => $params['title'],
                'status' => $params['status'],
            ];
            if (!empty($params['id']))
            {
                $saveData['u_time'] = datetime(time(), 'Y-m-d H:i:s');
                $res = ProductAttrModel::getInstance()->update($params['id'], $saveData);
                $attrId = $params['id'];
            }else{
                $saveData['c_time'] = datetime(time(), 'Y-m-d H:i:s');
                $attrId = $res = ProductAttrModel::getInstance()->insert($saveData);
            }
            Db::commit();
            return $res;
        }catch (Exception $e)
        {
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    /**
     * 获取产品特殊属性列表
     */
    public function getProductAttrList($params)
    {
        $page = $this->request->param('page', 1);
        $limit = $this->request->param('limit', 15);
        $start = ($page - 1) * $limit;
        $where = [];

        if (!empty($params['title']))
        {
            $where['title'] = ['LIKE', '%'.$params['title'].'%'];
        }
        $total = ProductAttrModel::getInstance()->count($where);
        $list = ProductAttrModel::getInstance()->select($where, '*', $start, $limit);
        return [
            'total' => $total,
            'list' => $list
        ];
    }

    /**
     * 获取产品特殊属性
     */
    public function getProductAttr($params)
    {
        $where = [];
        if (!empty($params['title']))
        {
            $where['title'] = ['LIKE', '%'.$params['title'].'%'];
        }
        $list = ProductAttrModel::getInstance()->select($where, 'id,title', 0, 100000);
        return $list;
    }

    /**
     * 获取产品特殊属性信息
     */
    public function getProductAttrInfo($params)
    {
        $Validate = new Validate([
            'attr_id' => 'require'
        ]);
        if (!$Validate->check($params))
        {
            $this->error($Validate->getError());
        }
        $info = ProductAttrModel::getInstance()->find($params['attr_id']);
        return $info;
    }
}